<?php

namespace App\Http\Controllers\Admin\Auth;

use App\Http\Models\Admin\Auth\RoleResource;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Responses\ResponseDto;
use App\Http\Models\Admin\Auth\Role;
use App\Http\Models\Admin\Auth\Resource;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
use Psy\Util\Json;

class RoleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        //
        //
        return View::make('admin.auth.role')->withUserinfo($request->session()->get('adminUser'))
            ->withUrltitle(trans("admin/message.adminusers_role"))
            ->withAssets($request['ASSETS']);
    }

    public function page(Request $request)
    {
        $datas = $request->input();
        $start = $datas[3]['value'];
        $length = $datas[4]['value'];

        $users = DB::table('role')->skip($start)->take($length)->get();
        $total = DB::table('role')->count();
        return ResponseDto::createResObj(ResponseDto::convertPageToDataTableObj($users,$total),ResponseDto::$SUCCESS);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $role_id = $request->input('id');
        $ids = DB::table('role_resource')->select('res_id')->where('role_id',$role_id)->get();

        $array = array_pluck($ids, 'res_id');
        $list = Resource::whereNull('parent_id')->orWhere('parent_id',0)->orWhere('parent_id',"")->orderBy('order_index','asc')->get();
        foreach($list as $item){
            $this->inTreeData($item,$array);
        }


        return Json::encode($list);
    }

    public function inTreeData($data,$values)
    {
        $data->children =  Resource::where('parent_id',$data->id)->orderBy('order_index','asc')->get();
        if($this->validateResource($data,$values)){
            $data->checked = true;
        }
        if($data->children == null){
            return;
        }

        foreach($data->children as $child){
            $this->inTreeData($child,$values);
        }

    }

    public function validateResource($data,$values)
    {
        if($values == null){
            return false;
        }
        foreach($values as $id){
            if($data->id == $id){
                return true;
            }
        }
        return false;

    }
    public function saveRoleResource(Request $request)
    {
        //
        $role_id = $request->input("role_id");
        $noids = $request->input("noids");
        $res_ids = array();
        if($noids != null && $noids != ""){
            $res_ids = explode(",",$noids);
        }

        RoleResource::where('role_id',$role_id)->delete();
        foreach($res_ids as $res_id){
            $roleResource = new RoleResource();
            $roleResource->role_id = $role_id;
            $roleResource->res_id = $res_id;
            $roleResource->save();
        }
        return ResponseDto::createResObj(trans("admin/message.update_success"), ResponseDto::$SUCCESS);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $validator = Validator::make(Input::all(),
        [
            'role_name' => 'required',

        ]);

        if ($validator->fails()) {
            return ResponseDto::createResObj($validator->errors()->first(), ResponseDto::$FAIL);
        } else {
            $role = new Role();
            $role->role_name = $request->input("role_name");
            $role->description = $request->input("description");

            if ($request->input("id")) {
                //更新操作
                $exit_item = Role::where('id', '<>', $request->input("id"))->where('role_name', $role->role_name)->first();
                if ($exit_item) {
                    return ResponseDto::createResObj(trans("admin/message.name_exit"), ResponseDto::$FAIL);
                } else {
                    $up_item = Role::find($request->input("id"));
                    $up_item->role_name = $role->role_name;
                    $up_item->description = $role->description;
                    if ($up_item->save()) {
                        return ResponseDto::createResObj(trans("admin/message.update_success"), ResponseDto::$SUCCESS);
                    } else {
                        return ResponseDto::createResObj(trans("admin/message.service_fail"), ResponseDto::$FAIL);
                    }
                }
            } else {
                //添加操作
                $find = Role::where('role_name', $request->input("role_name"))->first();
                if ($find) {
                    return ResponseDto::createResObj(trans("admin/message.name_exit"), ResponseDto::$FAIL);
                }
                if ($role->save()) {
                    return ResponseDto::createResObj(trans("admin/message.submit_success"), ResponseDto::$SUCCESS);
                } else {
                    return ResponseDto::createResObj(trans("admin/message.service_fail"), ResponseDto::$FAIL);
                }
            }


        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        return Json::encode(Role::find($id));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        Role::destroy($id);
        RoleResource::where('role_id',$id)->delete();
//        PostTags::where('tag_id',$id)->delete();
        return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);
    }
}
